home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 26 / CU Amiga Magazine's Super CD-ROM 26 (1998)(EMAP Images)(GB)[!][issue 1998-09].iso / CUCD / PowerPC / uae-0.8.4 / amiga / source / uae_rcli.c < prev    next >
C/C++ Source or Header  |  1997-11-03  |  4KB  |  202 lines

  1. ;/* compile with: execute uae_rcli.c
  2. failat 11
  3. sc uae_rcli.c opt link to uae_rcli noicons
  4. if ERROR
  5.    gcc uae_rcli.c -O3 -o uae_rcli -noixemul
  6. endif
  7. quit
  8. */
  9.  
  10. /*
  11.  *  uae_rcli.c - UAE remote cli
  12.  *
  13.  *  (c) 1997 by Samuel Devulder
  14.  */
  15.  
  16. #include <ctype.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include <proto/dos.h>
  22. #include <proto/exec.h>
  23. #include <libraries/dosextens.h>
  24.  
  25. #ifdef __GNUC__
  26. #include "../../src/include/uaeexe.h"
  27. #else
  28. #include "//src/include/uaeexe.h"
  29. #endif
  30.  
  31. #define NAME "uae_rcli"
  32. #define LEN 512
  33.  
  34. static int (*calltrap)(char *, int) = (void*)UAEEXE_ORG;
  35. static int delay  = 20; 
  36. static int debug  = 0;
  37. static int nofifo = 0;
  38. static ULONG fifo;
  39. static char buf[LEN];
  40.  
  41. /*
  42.  * lowlevel I/O
  43.  */
  44. static void WR(ULONG f,char *s)
  45. {
  46.     Write(f,s,strlen(s));
  47. }
  48. static void PR(char *s)
  49. {
  50.     WR(Output(),s);
  51. }
  52.  
  53. /*
  54.  * self explanatory
  55.  */
  56. static void usage(char *name)
  57. {
  58.     PR("Usage: ");PR(name);PR(" [-h|?] [-debug] [-nofifo] [<delay>]\n");
  59.     exit(0);
  60. }
  61.  
  62. /*
  63.  * grab options
  64.  */
  65. static void parse_cmdline(int ac, char **av)
  66. {
  67.     char *name = *av++;
  68.  
  69.     for(;--ac;++av) {
  70.         if(!strcmp(*av,"-debug"))  debug  = 1;        else
  71.         if(!strcmp(*av,"-nofifo")) nofifo = 1;        else
  72.         if(!strcmp(*av,"-h")) usage(name);            else
  73.         if(!strcmp(*av,"?"))  usage(name);            else
  74.         if(**av>='0' && **av<='9') delay = atoi(*av); else
  75.         {PR("Bad argument: \"");PR(*av);PR("\"\n");exit(0);}
  76.     }
  77.     if(!delay) delay = 1;
  78. }
  79.  
  80. /*
  81.  * See if command matches. Returns pointer to arguments.
  82.  */
  83. static char *match(char *src,char *cmd)
  84. {
  85.     while(*src == ' ' || *src == '\t') ++src;
  86.     while(*src && tolower(*src) == tolower(*cmd)) {++src;++cmd;}
  87.     while(*src==' ' || *src=='\t') ++src;
  88.     return (*cmd)?NULL:src;
  89. }
  90.  
  91. /*
  92.  * get command
  93.  */
  94. static int getcmd(void)
  95. {
  96.     if(debug) PR("-> Calltrap\n");
  97.     if(calltrap(buf, LEN-1)) {
  98.         if(debug) PR("-> 1\n");
  99.         return 1;
  100.     } else {
  101.         if(debug) PR("-> 0\n");
  102.         return 0;
  103.     }
  104. /*
  105.     PR(">> ");
  106.     if(fgets(buf,LEN-1,stdin) == NULL) strcpy(buf,"quit");
  107.     if(*buf=='\n') return 0;
  108.     return 1;
  109. */
  110. }
  111.  
  112. /*
  113.  * execute command
  114.  */
  115. static void my_exec(void)
  116. {
  117.     if(debug) {PR("-> Exec \"");PR(buf);PR("\"\n");}
  118.     if(fifo) {
  119.         WR(fifo,buf);
  120.         WR(fifo,"\n");
  121.     } else { /* nofifo => emulate cli */
  122.         char *s;
  123.         if((s=match(buf,"cd"))) {
  124.             if(*s==';' || !*s) {
  125.                 char buff[128];
  126.                 getcwd(buff,sizeof(buff)-1);
  127.                 PR(buff);PR("\n");
  128.             } else {
  129.                 chdir(s);
  130.             }
  131.         } else {
  132.             System(buf, NULL);
  133.         }
  134.     }
  135.     *buf = '\0';
  136. }
  137.  
  138. /*
  139.  * Open without requester
  140.  */
  141. ULONG myOpen(char *name, ULONG mode)
  142. {
  143.     ULONG rt, wd;
  144.     struct Process *pr;
  145.  
  146.     pr = (void*)FindTask(NULL);
  147.     if(pr->pr_Task.tc_Node.ln_Type != NT_PROCESS) return 0;
  148.  
  149.     wd = (ULONG)pr->pr_WindowPtr;
  150.     pr->pr_WindowPtr = (APTR)-1;
  151.     rt = Open(name,mode);
  152.     pr->pr_WindowPtr = (APTR)wd;
  153.  
  154.     return rt;
  155. }
  156.  
  157. /*
  158.  * Guess :)
  159.  */
  160. int main(int ac, char **av)
  161. {
  162.     int running = 1;
  163.  
  164.     parse_cmdline(ac,av);
  165.     if(!nofifo) {
  166.         if(debug) PR("-> Open fifo\n");
  167.         fifo = myOpen("fifo:"NAME"/wmke",MODE_NEWFILE);
  168.         if(!fifo) {
  169.             if(debug) PR("-> Starting fifo-handler\n");
  170.             System("run <nil: >nil: l:fifo-handler",NULL);
  171.             Delay(100);
  172.             if(debug) PR("-> Reopen fifo\n");
  173.             fifo = myOpen("fifo:"NAME"/wmke",MODE_NEWFILE);
  174.         }
  175.     }
  176.  
  177.     if(fifo) {
  178.         if(debug) PR("-> Spawning shell\n");
  179.         System("run execute fifo:"NAME"/rsk",NULL);
  180.         if(debug) WR(fifo,"echo \"-> Remote cli running\"\n");
  181.     } else if(debug) PR("-> No fifo found\n");
  182.  
  183.     do {
  184.         while(running && getcmd()) {
  185.             if(match(buf,"endcli"))   running = 0; else
  186.             if(match(buf,"endshell")) running = 0; else
  187.             if(match(buf,"quit"))     running = 0;
  188.         if(SetSignal(0L, SIGBREAKF_CTRL_C|SIGBREAKF_CTRL_D) & 
  189.                         (SIGBREAKF_CTRL_C|SIGBREAKF_CTRL_D)) {
  190.         running = 0;
  191.         }
  192.             if(running) my_exec();
  193.         }
  194.         if(running) Delay(delay);
  195.     } while(running);
  196.     if(debug) PR("-> Exiting\n");
  197.     (*calltrap)(0,0);
  198.     if(fifo) {
  199.         Close(fifo);
  200.     }
  201. }
  202.